<template>
    <div class="main-container login-container">
        <div class="login-main">
            <div class="title">
                <span class="logo">RENEW</span>
                系统后台登录
            </div>
            <el-form :model="userInfo" :rules="rules" ref="userInfo" @submit.native.prevent.stop="doLogin">
                <el-form-item prop="userName">
                    <i class="fa fa-user"></i>
                    <el-input v-model="userInfo.userName" size="large" placeholder="用户名"></el-input>
                </el-form-item>
                <el-form-item prop="pwd">
                    <i class="fa fa-lock"></i>
                    <el-input type="password" v-model="userInfo.pwd" size="large" placeholder="密码"></el-input>
                </el-form-item>
                <el-form-item prop="code" class="captcha">
                    <el-input v-model="userInfo.code" size="large" placeholder="验证码"></el-input>
                    <img class="captcha-img"
                        :src="'/login/getCode.do?v=' + version"
                        alt="" @click="version = (new Date()).getTime()">
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" native-type="submit" size="large">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
import hub from 'common/hub';
import {cookie} from 'common/util';
import {mapGetters} from 'vuex';
export default {
    components: {},
    mounted() {
        this.init();
    },
    data() {
        return {
            userInfo: {
                userName: '',
                pwd: '',
                code: ''
            },
            version: '',
            rules: {
                userName: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                pwd: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请输入验证码', trigger: 'blur' },
                    { len: 4, message: '请输入4位验证码', trigger: 'blur' }
                ]
            }
        };
    },
    computed: {
        ...mapGetters([
            'currentRoute'
        ])
    },
    methods: {
        init() {
            this.version = this.getNewTime();
            if (cookie.get('userid')) {
                this.$router.push('/');
            }
        },
        getNewTime() {
            return (new Date()).getTime();
        },
        doLogin() {
            this.$refs.userInfo.validate(valid => {
                if (valid) {
                    $.request('/login/doLogin.do', {
                        data: this.userInfo,
                        method: 'get'
                    }).then(res => {
                        console.log(this.userInfo)
                        cookie.set('userid', 1);
                        cookie.set('username', this.userInfo.userName);
                        hub.$emit('login', this.userInfo.userName);
                        this.$router.push('/');
                    }, res => {
                        this.version = this.getNewTime();
                        this.$message.error(res.statusInfo);
                    });
                }
            })
        }
    }
}
</script>
<style lang="less">
.main-container.login-container {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    background: #fff;
}
.login-main {
    width: 500px;
    padding: 0 30px 20px;
    transition: box-shadow 0.5s;
    box-shadow: 0 28px 40px rgba(0,0,0,0.2), 0 3px 20px rgba(0,0,0,0.15);
    .title {
        font-size: 20px;
        padding: 20px 0;
        .logo {
            color: #01a896;
        }
    }
    .fa {
        position: absolute;
        z-index: 20;
        top: 13px;
        left: 12px;
        font-size: 18px;
        color: #aaa;
    }
    .el-input .el-input__inner {
        padding-left: 34px;
    }
    .captcha {
        .el-input {
            width: 65%;
            display: inline-block;
            vertical-align: middle;
            .el-input__inner {
                padding-left: 10px;
            }
        }
        .captcha-img {
            vertical-align: middle;
            cursor: pointer;
            margin-left: 12px;
            border: 1px solid #cccccc;
            border-radius: 3px;
        }
    }
    .el-button {
        width: 100%;
    }
}

</style>
